//
//  BaseViewController.m
//  SpeedBorrow
//
//  Created by  刘尚威 on 16/8/26.
//  Copyright © 2016年  刘尚威. All rights reserved.
//

#import "XLBaseViewController.h"
#import "TYAlertController.h"
#import "Lothar+Main.h"
#import "Lothar+Login.h"
#import "FunProgressHUD.h"

//#import <Main_Category/Lothar+Main.h>
//#import <Login_Category/Lothar+Login.h>
//#import <BaseModule/FunProgressHUD.h>
@interface XLBaseViewController ()<UIGestureRecognizerDelegate>

@end

@implementation XLBaseViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.view.backgroundColor = kBackground_color;
    
    //初始化页面所需数据
    [self initData];
    
    //创建页面其余部分
    [self initInterface];
    
    //返回按钮
//    if (self.navigationController.viewControllers.count > 1) {
//        [self addLeftButtonItem];
//    }
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [self dismissHUD];
}

#pragma mark - Public Method
#pragma mark 错误提示
- (void)exitLogin:(NSString *)msg {
    NSString *tipMessage = ISNILORNULL(msg);
    if ([tipMessage containsString:LOGIN_STR]) {
        UserInfo.isLogin = NO;
        [UserInfo synchronize];
        [self dismissHUD];
        
        [self showLoginAlert];
        
    }else{
        [self showErrorWithStatus:[NSString stringWithFormat:@"%@",tipMessage]];
    }
}

- (void)showLoginAlert {
    TYAlertView *alertView = [TYAlertView alertViewWithTitle:@"提示" message:@"您已被迫下线，请重新登录!"];
    [alertView addAction:[TYAlertAction actionWithTitle:@"去登陆" style:TYAlertActionStyleDefault handler:^(TYAlertAction *action) {
        
        UIViewController *loginVC = [[Lothar shared]login_aViewController];
        [[Lothar shared] main_setNavBarRootViewController:loginVC];
        
    }]];
    TYAlertController *alertController = [TYAlertController alertControllerWithAlertView:alertView preferredStyle:TYAlertControllerStyleAlert transitionAnimation:TYAlertTransitionAnimationScaleFade];
    [self presentViewController:alertController animated:YES completion:nil];
}

#pragma mark 初始化数据
-(void)initData
{
    //没有数据请求的子类 不要调用[super initData];
    [self showHUD];
}

#pragma mark 创建界面
-(void)initInterface
{
    
}

#pragma mark 提示
- (void)showHUD {
    if (![FunProgressHUD isVisible]) {
        [FunProgressHUD show];

    }
}


- (void)dismissHUD {
    if ([FunProgressHUD isVisible]) {
        [FunProgressHUD dismiss];
    }
}

- (void)showSuccessWithStatus:(NSString *)status {
    [FunProgressHUD showSuccessWithStatus:status];
}

- (void)showErrorWithStatus:(NSString *)status {
    [FunProgressHUD showErrorWithStatus:status];
}

- (void)showWithStatus:(NSString *)status {
    [FunProgressHUD showLodingWithStatus:status];
}

- (void)showInfoWithStatus:(NSString *)status {
    [FunProgressHUD showInfoWithStatus:status];
}


#pragma mark 创建barButtonItem

- (void)setupRightBarItemWithTitle:(NSString *)buttonTitle action:(SEL)target {
    self.rightBarButtonItem = [[FunUIBarButtonItem alloc]initWithButtonTitle:buttonTitle titleColor:[UIColor whiteColor] buttonFrame:CGRectMake(0, 0, 50, 30) target:self action:target];
    self.navigationItem.rightBarButtonItems = [self.rightBarButtonItem setTranslation:self.rightBarButtonItem translation:-11];
}

#pragma mark - Setter
- (void)setRightBarButtonItemEnable:(BOOL)rightBarButtonItemEnable {
    _rightBarButtonItemEnable = rightBarButtonItemEnable;
    if (rightBarButtonItemEnable) {
        [self.rightBarButtonItem buttonIsCanClick:YES buttonColor:[UIColor whiteColor] barButtonItem:self.rightBarButtonItem];
    } else {
        [self.rightBarButtonItem buttonIsCanClick:NO buttonColor:kXLButtonTextColorDisable barButtonItem:self.rightBarButtonItem];
    }
}

/*
 -(void)addLeftButtonItem {
    UIButton *btnLeft = [UIButton buttonWithType:UIButtonTypeCustom];
    [btnLeft setImage:XLImage(@"nav_backArrow") forState:UIControlStateNormal];
//    [btnLeft setImage:[UIImage imageNamed:nameSelected] forState:UIControlStateSelected];
//    [btnLeft setImage:[UIImage imageNamed:nameSelected] forState:UIControlStateHighlighted];
    [btnLeft setFrame:CGRectMake(-10, 0, 0.08 *KScreenWidth, 0.117 *KScreenHeight)];
    btnLeft.contentMode = UIViewContentModeCenter;
    [btnLeft setImageEdgeInsets:UIEdgeInsetsMake(0, -15, 0, 0)];
    [btnLeft addTarget:self action:@selector(leftBarButtonItemEvent:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *leftItem = [[UIBarButtonItem alloc] initWithCustomView:btnLeft];
    self.navigationItem.leftBarButtonItem = leftItem;
}
 */

- (void)leftBarButtonItemEvent:(UIButton *)sender {
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
